from typing import Dict, Optional
from fastapi import FastAPI, Path, Query, Body
import uvicorn
from pydantic import BaseModel, Field

app = FastAPI()

'''
     Fields，它针对 Pydantic Model 内部字段进行额外的校验和添加元数据
     
     Fields它是 Pydantic 提供的方法，并不是 FastAPi 提供的哦
    该方法返回了一个实例对象，是 Pydantic 中 FieldInfo 类的实例对象

'''


class Item(BaseModel):
    name: str
    description: Optional[str] = Field(
        default=None,
        title='标题',
        max_length=5
    )
    price: float = Field(..., gt=0, description="大于0")
    tax: Optional[float] = None


class User(BaseModel):
    username: str
    full_name: Optional[str] = None


@app.post("/items/{item_id}")
async def update_item(item_id: int, item: Item = Body(..., embed=True)):
    results = {"item_id": item_id, "item": item}
    return results


if __name__ == '__main__':
    uvicorn.run(app='main:app', host='127.0.0.1', port=8000, reload=True, debug=True)
